﻿using SQ.BLL.Factory;
using SQ.BLL.Implementation;
using SQ.BLL.Interfaces;
using SQ.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SQ.API.Controllers
{
    [RoutePrefix("api/estabelecimento")]
    public class EstabelecimentoController : ApiController
    {
        private IEstabelecimentoBS estabelecimentoBS = BLLFactory.getEstabelecimentoBS();

        public HttpResponseMessage Get()
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, this.estabelecimentoBS.ObterEstabelecimentos());
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("{email}")]
        public HttpResponseMessage Get(string email)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, this.estabelecimentoBS.ObterEstabelecimentoPorEmail(email));
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [HttpPost]
        public HttpResponseMessage Post(Estabelecimento estabelecimento)
        {
            if (estabelecimento == null)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Estabelecimento Nulo");
            }

            try
            {
                this.estabelecimentoBS.CriarEstabelecimento(estabelecimento);
                return Request.CreateResponse(HttpStatusCode.OK, estabelecimento);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [HttpPut]
        public HttpResponseMessage Put(Estabelecimento estabelecimento)
        {
            if (estabelecimento == null)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Estabelecimento Nulo");
            }

            try
            {
                this.estabelecimentoBS.AlterarEstabelecimento(estabelecimento);
                return Request.CreateResponse(HttpStatusCode.OK, estabelecimento);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [HttpDelete]
        [Route("{codigoEstabelecimento}")]
        public HttpResponseMessage Delete(int codigoEstabelecimento)
        {
            if (codigoEstabelecimento == 0)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Estabelecimento não informado");
            }

            try
            {
                this.estabelecimentoBS.ExcluirEstabelecimento(codigoEstabelecimento);
                return Request.CreateResponse(HttpStatusCode.OK, "Estabelecimento Excluído");
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
    }
}